import 'dart:math';

import 'package:uuid/uuid.dart';
import 'package:uuid/uuid_util.dart';

class StringUtil {
  static const double Earth_Equatorial_Radius = 6378137.0;

  // /**
  //  * 获取MD5编码格式
  //  */
  // static String createMD5Str(String data) {
  //   var content = new Utf8Encoder().convert(data);
  //   var digest = md5.convert(content);
  //   // 这里其实就是 digest.toString()
  //   return hex.encode(digest.bytes).toUpperCase();
  // }

  /**
   * 获取UUID
   */
  static String createUUID() {
    var uuid = const Uuid(options: {'grng': UuidUtil.cryptoRNG});
    String id = uuid.v4();
    id = id.replaceAll("-", "").toUpperCase();
    return id;
  }

  /**
   * 根据经纬度坐标计算 dns瓦片号
   *
   * @param nLevel dns瓦片的级别
   * @param x      精度坐标
   * @param y      纬度坐标
   * @return 瓦片号
   */
  static int getTileId(int nLevel, double x, double y) {
    int nTileID;
    int p;
    int nX = (x / 90 * (1 << 30)).toInt();
    int nY = (y / 90 * (1 << 30)).toInt();

    nTileID = nX < 0 ? 1 : 0;

    for (p = 30; p > (30 - nLevel); p--) {
      nTileID <<= 1;
      if ((nY & (1 << p)) != 0) {
        nTileID |= 1;
      }
      nTileID <<= 1;
      if ((nX & (1 << p)) != 0) {
        nTileID |= 1;
      }
    }
    nTileID += (1 << (16 + nLevel));

    return nTileID;
  }

  // //瓦片计算
  // static int getTileIdFromWkt(int nLevel, String wkt) {
  //   GeoJson geoJson = GeoJson.parseWkt(wkt);
  //   if (geoJson is GeoJsonPoint) {
  //     return getTileId(nLevel, geoJson.coordinates[0].toDouble(),
  //         geoJson.coordinates[1].toDouble());
  //   }else if(geoJson is GeoJsonLineString){
  //     return getTileId(nLevel, geoJson.coordinates[0][0].toDouble(),
  //         geoJson.coordinates[0][1].toDouble());
  //   }else if(geoJson is GeoJsonPolygon){
  //     return getTileId(nLevel, geoJson.coordinates[0][0][0].toDouble(),
  //         geoJson.coordinates[0][0][1].toDouble());
  //   }
  //   return 1;
  // }

  /**
   *  计算包围盒长宽(地理坐标包围盒)
   *
   *  @param pt1
   *  @param pt2
   *
   *  return 距离
   *
   */
  static num computeDistanceFromGeo(
      double lon, double, lat, double lon2, double lat2) {
    num lonDis = lon - lon2;
    num latDis = lat - lat2;
    num dis = 2 *
        asin(sqrt(pow(sin(lonDis / 2), 2) +
            cos(lon) * cos(lon2) * pow(sin(latDis / 2), 2)));
    dis = dis * Earth_Equatorial_Radius; //地球半径
    return dis;
  }

  Future<List<double>> strToDouble(String geometry, String nextGeometry) async {
    List<double> list = [];
    if (geometry != null &&
        geometry.length > 6 &&
        geometry.contains("POINT") &&
        nextGeometry != null &&
        nextGeometry.length > 6 &&
        nextGeometry.contains("POINT")) {
      String lon = geometry.substring(7, geometry.indexOf(" ", 7));
      String lat =
          geometry.substring(geometry.indexOf(" ", 7), geometry.length - 1);
      if (lon != null && lon.length > 0 && lat != null && lat.length > 0) {
        list.add(double.parse(lon));
        list.add(double.parse(lat));
      }
      String lon2 = nextGeometry.substring(7, nextGeometry.indexOf(" ", 7));
      String lat2 = nextGeometry.substring(
          nextGeometry.indexOf(" ", 7), nextGeometry.length - 1);
      if (lon2 != null && lon2.length > 0 && lat2 != null && lat2.length > 0) {
        list.add(double.parse(lon2));
        list.add(double.parse(lat2));
      }
    }

    return list;
  }

  /**
   * 小数格式化
   * @num 原始值
   * @int 保留小数位数
   * @return 字符串
   * */
  static String formatNum(num num, int postion) {
    if ((num.toString().length - num.toString().lastIndexOf(".") - 1) <
        postion) {
      //小数点后有几位小数
      print(num.toStringAsFixed(postion)
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString());
      return num.toStringAsFixed(postion)
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    } else {
      print(num.toString()
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString());
      return num.toString()
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    }
  }

  // static String formatTime(int milliseconds) {
  //   DateTime now = DateTime.fromMillisecondsSinceEpoch(milliseconds); //获取当前时间
  //   String formattedDate =
  //       DateFormat('yyyy-MM-dd HH:mm:ss').format(now); //格式化日期
  //   return formattedDate;
  // }
  //
  // static String formatTime2(int milliseconds) {
  //   DateTime now = DateTime.fromMillisecondsSinceEpoch(milliseconds); //获取当前时间
  //   String formattedDate =
  //   DateFormat('yyyy.MM.dd HH:mm:ss').format(now); //格式化日期
  //   return formattedDate;
  // }

  ///返回当前YYYYMMDDHHMMSS
  static String formatCurrentTime() {
    DateTime now = DateTime.now();
    return "${now.year}${now.month}${now.day}${now.hour}${now.minute}${now.millisecond}";
  }
}
